#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Solution {
	int min_sum(vector<int> &v, int s, int t, vector<vector<int>> &recd)
	{
                if(recd[s][t] != -1){
                        return recd[s][t];
                }
                if(s == t || s + 1 == t){
                        return recd[s][t] = 0;
                }
                int ans = INT32_MAX;
                for(int i = s + 1; i < t; i++){
                        int sum = v[s] * v[t] * v[i];
                        sum += min_sum(v, s, i, recd);
                        sum += min_sum(v, i, t, recd);
                        ans = min(ans, sum);
                }
                return recd[s][t] = ans;
	}

        int min_sum(vector<int> &v)
        {
                int n = v.size();
                vector<vector<int>> dp(n, vector<int>(n, INT32_MAX));
                for(int i = 0; i < n; i++){
                        dp[i][i] = 0;
                        if(i + 1 < n) dp[i][i + 1] = 0;
                        if(i + 2 < n) dp[i][i + 2] = v[i] * v[i + 1] * v[i + 2];
                }

                for(int j = 3; j < n; j++){
                        for(int s = 0 , t = j; t < n; s++, t++){
                                for(int k = s + 1; k < t; k++){
                                        dp[s][t] = min(dp[s][t], v[s] * v[t] * v[k] + dp[s][k] + dp[k][t]);
                                }
                        }
                }
                return dp[0][n - 1];
        }

    public:
	int minScoreTriangulation(vector<int> &values)
	{
                // int n = values.size();
                // vector<vector<int>> recd(n, vector<int>(n, -1));
                // return min_sum(values, 0, n - 1, recd);
                return min_sum(values);
	}
};

int main()
{
        vector<int> values = {3,7,4,5};
        cout<<Solution().minScoreTriangulation(values)<<endl;
	return 0;
}